<?php
namespace data\set;
use \data\BaseData;
/**
 * 标签
 */
class TagsData extends BaseData{
    
    
    public function find($param){
        $check = ['id','tagname','siteid'];
        $where = $this->parseWhere($check,$param);
        if( !$where )  dataE('查询条件不能为空');
        $info  = M('set_tags')->where($where)->find();
        return $info;
    }
    
    /**
     * 添加
     */
    public function add($data){
        $check = [            
            'siteid'    => ['reg'=>'int'],
            'status'    => ['val'=>1],
            'sort'      => ['def'=> 50,'reg'=>'int'],
            'tagname'   => ['check'=>'must','msg'=> '请填写标签名称'],
        ];        
        $save = $this->verifyData($check,$data);
        $id = M('set_tags')->insertGetId($save);
        if( !$id )  dataE('添加标签失败');
        return $id;
    }
    
    /**
     * 编辑
     */
    public function edit($id,$data){ 
        if( !$id ) dataE('修改参数不能为空');
        $check = [            
            //'siteid'    => ['reg'=>'int'],
            'status'    => ['reg'=>[1,2]],
            'sort'      => ['reg'=>'int'],
            'tagname'   => ['reg'=>'must','msg'=> '请填写标签名称'],
        ];    
        $save = $this->verifyData($check,$data);
        if( !$save ) return false;
        $where = ['id'=>$id];
        $result = M('set_tags')->where($where)->update($save);
        if($result === false ) dataE('修改标签失败');
        return $result;
    }
    

    
    
    /**
     * 列表读取
     */
    public function getSimpleList($param){
        $page = $param['page']?:1;
        $limit= $param['limit']?:99;
        $check = ['siteid','status'];
        $where = $this->parseWhere($check,$param);
        $list  = M('set_tags')->where($where)->limit($limit)->page($page)->order('sort desc,id desc')->select();
        return $list;
    }
}